def findMedianSortedArrays(nums1: [int], nums2: [int]) -> float:
    def getKthElement(k):
        index1, index2 = 0, 0
        while True:
            if index1 == m:
                return nums2[index2 + k - 1]
            if index2 == n:
                return nums1[index1 + k - 1]
            if k == 1:
                return min(nums1[index1], nums2[index2])

            newIndex1 = min(index1 + k // 2 - 1, m - 1)
            newIndex2 = min(index2 + k // 2 - 1, n - 1)
            pivot1, pivot2 = nums1[newIndex1], nums2[newIndex2]
            if pivot1 <= pivot2:
                k -= newIndex1 - index1 + 1
                index1 = newIndex1 + 1
            else:
                k -= newIndex2 - index2 + 1
                index2 = newIndex2 + 1


    m, n = len(nums1), len(nums2)
    totalLength = m + n
    if totalLength % 2 == 1:
        return getKthElement((totalLength + 1) // 2)
    else:
        return (getKthElement(totalLength // 2) + getKthElement(totalLength // 2 + 1)) / 2


if __name__ == '__main__':
    print(findMedianSortedArrays([1,2],[-1,3]))
    print(findMedianSortedArrays(nums1 = [2], nums2 = []))
    print(findMedianSortedArrays(nums1=[1, 3], nums2=[2]))
    print(findMedianSortedArrays(nums1=[1, 2], nums2=[3, 4]))
    print(findMedianSortedArrays(nums1=[0, 0], nums2=[0, 0]))
